#include <string.h>
#include <stdio.h>
#include "mhscpu.h"
#include "ring_buffer.h"
#include "dma_test.h"
#include "uart_test.h"

extern void SER_Init (void);
extern UART_TypeDef *PRINT_UART; 

void NextTestSetp(void);
void PrintTestList(void);

RingBufferTypeDef ringbuf;

uint32_t flagRece = 0;
uint32_t flagSend = 0;


uint8_t u8USARTGetDate[1024] = {0};
uint32_t u32USARTGetDateIndex = 0;
uint32_t u32USARTGetDateNum = 256;

uint32_t sysclk[] = {SYSCTRL_PLL_96MHz,SYSCTRL_PLL_72MHz};
uint32_t hclk[] = {SYSCTRL_HCLK_Div_None,SYSCTRL_HCLK_Div2,SYSCTRL_HCLK_Div4};
uint32_t pclk[] = {SYSCTRL_PCLK_Div2,SYSCTRL_PCLK_Div4};
SYSCTRL_ClocksTypeDef sclock;

int main(void)
{
	int32_t i,j,k;

	SYSCTRL_APBPeriphClockCmd(SYSCTRL_APBPeriph_GPIO,ENABLE);

// 	SYSCTRL_SYSCLKConfig(SYSCLK_96MHz);
// 	SYSCTRL_HCLKConfig(SYSCTRL_HCLK_Div_None);
// 	SYSCTRL_PCLKConfig(SYSCTRL_PCLK_Div4);
	
	GPIO_PinRemapConfig(GPIOA, GPIO_Pin_0 | GPIO_Pin_1, GPIO_Remap_0);
	
	SER_Init();
	PrintTestList();
	
	printf("Select Test Item(Using ASSII Number)\n\n");

// 	UART_DMA_Test();
	printf("Please Connect UART1 and UART2,Befor Test\n");
	NextTestSetp();

// 	SYSCTRL_GetClocksFreq(&sclock);
// 	SER_Init();
// 	printf("\n");
// 	printf("sysclk is %d\n", sclock.PLL_Frequency);
// 	printf("hclk is %d\n", sclock.HCLK_Frequency);
// 	printf("pclk is %d\n", sclock.PCLK_Frequency);
// 	printf("Test Resualt:\n");
// 	UART_DMA_Test();
// 	goto END;
	
	for (i = 0; i < sizeof(sysclk)/sizeof(sysclk[0]);i++)
	{
		SYSCTRL_PLLConfig((SYSCTRL_PLL_TypeDef)sysclk[i]);
		for (j = 0; j < sizeof(hclk)/sizeof(hclk[0]); j++)
		{
			SYSCTRL_HCLKConfig(hclk[j]);
			for (k = 0;k < sizeof(pclk)/sizeof(pclk[0]);k++)
			{
				SYSCTRL_PCLKConfig(pclk[k]);
				SYSCTRL_GetClocksFreq(&sclock);
				SER_Init();
				printf("\n");
				printf("sysclk is %d\n", sclock.PLL_Frequency);
				printf("hclk is %d\n", sclock.HCLK_Frequency);
				printf("pclk is %d\n", sclock.PCLK_Frequency);
				printf("Test Resualt:\n");
				UART_DMA_Test();
			}
		}

	}
	
END:
	printf("Test End\n");
	while(1);
}

void PrintTestList(void)
{
	printf("====================MegaHunt SCPU Test===================\n");
	printf("Test Item List:\n");
	printf("1:DMA Test (On Uart)\n");
	printf("2:UART Test\n");
	printf("==========================List End=======================\n\n");
}


void NextTestSetp(void)
{
	int32_t tmpRece;
	printf("Send ASSII \"n\" and Continue Next Step\n\n");
	while(TRUE)
	{
		if(UART_GetLineStatus(PRINT_UART) & UART_LINE_STATUS_RX_RECVD)
		{
			tmpRece = UART_ReceiveData(PRINT_UART);
			if('N' == tmpRece || 'n' == tmpRece)
				break;
		}
	}
}

void NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	
	NVIC_SetVectorTable(NVIC_VectTab_RAM,0);
	NVIC_SetPriorityGrouping(NVIC_PriorityGroup_0);

	NVIC_InitStructure.NVIC_IRQChannel = UART0_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	NVIC_InitStructure.NVIC_IRQChannel = UART1_IRQn;
	NVIC_Init(&NVIC_InitStructure);
}

void UART_Configuration(void)
{
	UART_InitTypeDef UART_InitStructure;
	UART_FIFOInitTypeDef UART_FIFOInitStruct;

	UART_StructInit(&UART_InitStructure);
	UART_FIFOStructInit(&UART_FIFOInitStruct);
	
	UART_InitStructure.UART_BaudRate = 115200;
	UART_InitStructure.UART_WordLength = UART_WordLength_8b;
	UART_InitStructure.UART_StopBits = UART_StopBits_1;
	UART_InitStructure.UART_Parity = UART_Parity_No;
	
	UART_FIFOInitStruct.FIFO_Enable = ENABLE;
	UART_FIFOInitStruct.FIFO_DMA_Mode = UART_FIFO_DMA_Mode_1;
	UART_FIFOInitStruct.FIFO_RX_Trigger = UART_FIFO_RX_Trigger_1_4_Full;
	UART_FIFOInitStruct.FIFO_TX_Trigger = UART_FIFO_TX_Trigger_1_4_Full;
	UART_FIFOInitStruct.FIFO_TX_TriggerIntEnable = ENABLE;

	UART_Init(UART0, &UART_InitStructure);
	UART_Init(UART1, &UART_InitStructure);
}


#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
	/* User can add his own implementation to report the file name and line number,
	 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

	/* Infinite loop */
	while (1)
	{
	}
}
#endif






